package com.baihan.angus.app.game.busshow.ui.user.album;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.baihan.angus.app.game.basic.base.view_model.BaseViewModel;
import com.baihan.angus.app.game.basic.utils.Tooast;
import com.baihan.angus.app.game.basic.utils.net.ApiObserver;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.response.AlbumResponse;
import com.baihan.angus.app.game.common.bean.response.CreatePhotoResponse;
import com.baihan.angus.app.game.common.bean.response.UploadMultipleResponse;
import com.baihan.angus.app.game.common.data_center.remote.RemoteDataMng;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class AlbumVM extends BaseViewModel {

    public AlbumVM(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<UploadMultipleResponse> mldMultiple = new MutableLiveData<>();
    public MutableLiveData<AlbumResponse.Data> mldAlbum = new MutableLiveData<>();

    public void transferFile(List<String> paths) {
        List<File> files = new ArrayList<>();
        for (int i = 0; i < paths.size(); i++) {
            String path = paths.get(i);
            File file = new File(path);
            files.add(file);
        }

        uploadFileList(files);
    }

    public void uploadFileList(List<File> files) {
        showLoading("uploadFileList");
        List<MultipartBody.Part> parts = new ArrayList<>();
//        RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), "file");
        for (int i = 0; i < files.size(); i++) {
            File file = files.get(i);
            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            MultipartBody.Part body = MultipartBody.Part.createFormData("file[]", file.getName(), requestFile);
//                                    MultipartBody.Part.createFormData("file" + (i + 1), file.getName(), requestFile);
            parts.add(body);
        }

        RemoteDataMng.getInstance().uploadFileList(parts, mLifecycleProvider.bindToLifecycle(), new ApiObserver<UploadMultipleResponse>() {
            @Override
            public void onSuccess(UploadMultipleResponse uploadMultipleResponse) {
                hideLoading("uploadFileList");
                mldMultiple.setValue(uploadMultipleResponse);
                Tooast.warning(getApplication().getString(R.string.uploadSuccess));
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("uploadFileList");
                Tooast.warning(getApplication().getString(R.string.uploadFail));
                mldMultiple.setValue(null);
            }
        });
    }

    public void getPhotoList() {
        showLoading("getPhotoList");
        RemoteDataMng.getInstance().getPhotoList(mLifecycleProvider.bindToLifecycle(), new ApiObserver<AlbumResponse>() {
            @Override
            public void onSuccess(AlbumResponse albumResponse) {
                hideLoading("getPhotoList");
                mldAlbum.setValue(albumResponse.getData());
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("getPhotoList");
                Tooast.warning(msg);
                mldAlbum.setValue(null);
            }
        });
    }

    public void createPhotoList(Map<String, Object> pMap) {
        showLoading("createPhotoList");
        RemoteDataMng.getInstance().createPhotoList(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<CreatePhotoResponse>() {
            @Override
            public void onSuccess(CreatePhotoResponse createPhotoResponse) {
                hideLoading("createPhotoList");
                finish();
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("createPhotoList");
                Tooast.warning(msg);
            }
        });
    }
}
